<?php

class ProductLines extends HTech_Db_Table implements Emms_Interface_Performance
{
    protected $_name    = 'product_lines';
    protected $_primary = 'ProductLineId'; 
    
    protected $_dependentTables = array('Programs');
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Lines");
        $this->_gridMetadata['onClick'] = 'admin.product-line.view';
        $this->_gridMetadata['headers'] = array
            (
                array('ProductLineId',      50, $this->translate("Id"),             'Number'),
                array('Code',              200, $this->translate("Code"),           'String'),
                array('ProductLine',       200, $this->translate("Line"),           'String'),
                array('LastEditDate',      150, $this->translate("Last edit date"), 'DateTime', 'Date')
            );
        return $this;
    }
    
    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('programs' => null);
        $this->_record->r->programs = $this->_record->d->findDependentRowset('Programs');  
    }
    
    protected function _setRecordPerformance() 
    {
        $this->_record->p = $this->getPerformance($this->_record->d, new Zend_Date());
    }
       
    static function getLineOptions($type = '', $id = 0)
    {
        if(!$id)            
            return array('' => '--- ' . self::translate('Select line')) + 
                Zend_Registry::get('db')->fetchPairs("select ProductLineId, ProductLine 
                    from product_lines order by ProductLine");
        return array();
    }    
    
    public function getPerformanceCharts(Zend_Db_Table_Row $row)
    {   
        return array(
            'title' => $row['ProductLine'],
            'subtitle' => self::translate('Performance charts'),
            'params' => array(
                'p1' => 'LineId',
                'v1' => $row['ProductLineId'],
                'p2' => 'ProductLine',
                'v2' => $row['Code']),
            'charts' => array(
                'day' => array(
                    'chartId' => 'LinePerformanceDay',
                    'chartData' => '/admin/chart/performance/Mode/Day/Subject/Line/id/' . $row['ProductLineId'],
                    'chartSizeX' => 350,
                    'chartSizeY' => 320),
                'week' => array(
                    'chartId' => 'LinePerformanceWeek',
                    'chartData' => '/admin/chart/performance/Mode/Week/Subject/Line/id/' . $row['ProductLineId'],
                    'chartSizeX' => 200,
                    'chartSizeY' => 320),
                'month' => array(
                    'chartId' => 'LinePerformanceMonth',
                    'chartData' => '/admin/chart/performance/Mode/Month/Subject/Line/id/' . $row['ProductLineId'],
                    'chartSizeX' => 600,
                    'chartSizeY' => 320)
            )
        );
    }
    
    public function getPerformance(Zend_Db_Table_Row $row, Zend_Date $date, $options = array())
    {
        $row = $row; $date = $date; $options = $options;
        
        $ps = array();
        $pc = array();
        
        $pf = Zend_Registry::get('db')->query("select * from performance_data where Date = curdate() and LineId = ?", 
                array($row['ProductLineId']))->fetchAll();
        
        foreach($pf as $metric) {
            $ps[$metric->Metric] = $metric->Value;
            $pc[$metric->Metric] = $metric->Color;
        }
        $ps['c'] = (object) $pc;
        return (object) $ps;
    }
}